<?php

namespace Admin\Controller;

class ReportingController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Instore");
    }

    public function workorder() {
        $requestMap = $this->convertRequestToMap();

        $this->assign("begin_date", $_REQUEST["s#outstore_date-egt"]);
        $this->assign("end_date", $_REQUEST["s#outstore_date-elt"]);

        if(!isNullOrEmpty($requestMap["instore_month"])){
            $month = $requestMap["instore_month"];
            unset($requestMap["instore_month"]);
            $requestMap["s.storage_time-egt"] = $month . "-01 00:00:00";
            $requestMap["s.storage_time-elt"] = $month . "-31 23:59:59";
            $this->assign("begin_date", "");
            $this->assign("end_date", "");
        }

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["s.enterprise_code"] = $user["enterprise_code"];

        //获取列表
        $list = D("Instore")->alias("s")
//            ->join("left join __ADMIN__ emp on emp.employee_id = s.update_id")
            ->join("left join __WORKORDER__ wo on wo.workorder_id = s.product_id")
            ->join("left join __PATIENT__ pt on pt.patient_id = wo.patient_id")
            ->join("left join __STORE__ st on st.store_id = s.store_id")
            ->join("left join __ADMIN__ sales on sales.employee_id = wo.sales_id")
            ->join("left join __ADMIN__ storeman on storeman.employee_id = wo.storeman_id")
            ->join("left join __ADMIN__ auditor on auditor.employee_id = s.outstore_auditor_id")
            ->where(getWhereSqlByMap($requestMap))
            ->order("s.create_time desc")
//            ->limit()
            ->field("s.*, pt.patient_name, pt.sex, pt.age, sales.admin_name as sales_name, auditor.admin_name as auditor_name,storeman.admin_name as storeman_name, wo.workorder_no, wo.price, wo.sales_id, st.store_name, wo.payment_status, wo.deposit")
            ->select();

        for($i = 0; $i < count($list); $i++){
            $materialList = D("ClaimMaterial")->where(array('workorder_no' => $list[$i]["workorder_no"], 'status' => 1, 'enterprise_code'=>$user["enterprise_code"]))->select();
            $list[$i]["cost"] = 0;
            foreach($materialList as $material){
                $list[$i]["cost"] += $material["total_price"];
            }
        }

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("user", $user);
        $this->display();

    }

    public function getTable($name)
    {
        switch ($_REQUEST["name"]) {
            case 'workorder':
                self::getWorkorderTable($_REQUEST["id"]);
                break;
        }
    }

    private function getWorkorderTable($ids)
    {
//        $data = self::getData();
        $map['class_name'] = "reporting_workorder";
        $formDict = D("FormDict")->queryForList($map);

        $data = array();
        $data["header"] = array();
        $data["volume"] = array();
        $data["list"] = array();
        foreach ($formDict as $key => $value){
            array_push($data["header"], $value);
            array_push($data["volume"], $key);
        }

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        //获取列表
        $list = D("Instore")->alias("s")
//            ->join("left join __ADMIN__ emp on emp.employee_id = s.update_id")
            ->join("left join __WORKORDER__ wo on wo.workorder_id = s.product_id")
            ->join("left join __STORE__ st on st.store_id = s.outstore_store_id")
            ->join("left join __ADMIN__ sales on sales.employee_id = wo.sales_id")
            ->where(array('s.enterprise_code' => $user["enterprise_code"]))
//            ->limit()
            ->field("s.*, sales.admin_name, wo.workorder_no, wo.price, st.store_name")
            ->select();

//        $ids = $_REQUEST["id"];
        $idArr = explode(',', $ids);
        foreach ($list as $item){
            if(in_array($item["product_id"], $idArr)){
                $materialList = D("ClaimMaterial")->where(array('workorder_no' => $item["workorder_no"], 'status' => 1, 'enterprise_code' => $user["enterprise_code"]))->select();
                $item["cost"] = 0;
                foreach ($materialList as $material) {
                    $item["cost"] += $material["total_price"];
                }
                if(isNullOrEmpty($item["outstore_name"])){
                    $item["outstore_name"] = $item["inventory_name"];
                }

                if($item["outstore_status"] == 1){
                    $item["outstore_status"] = "已审核";
                }else{
                    $item["outstore_status"] = "待审核";
                }
                array_push($data["list"], $item);
            }
        }
        $fileName = "定制产品报表_".date('Ymd',time());
        exportExcel($data, $fileName);
    }

    public function audit() {
        $data["code"] = 200;
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        $idArr = explode(',', $_REQUEST["id"]);
        if(sizeof($idArr) == 0){
            $data["message"] = "无可审核订单。";
            echo json_encode($data);
            return;
        }
        $count = 0;
        foreach ($idArr as $id) {
            $instoreData = D("Instore")->queryById($id);
            if($instoreData["outstore_status"] != -1){
                continue;
            }
            $instoreData["outstore_status"] = 1;
            $instoreData["outstore_auditor_id"] = $user["employee_id"];
            $instoreData["outstore_audit_time"] = getCurrentDate();
            D("Instore")->updateInfo($instoreData);
            $count++;
        }
        $notifyData["first"] = "定制产品订单批量财务审核通过。";
        $notifyData["keyword1"] = "共 ".$count." 单";
        $notifyData["keyword2"] = "审核通过";
        $notifyData["keyword3"] = "财务审核人：" . $user["admin_name"];
        $notifyData["keyword4"] = getCurrentDate();
        $notifyData["remark"] = null;

        $openidArray = getNotifyArrByName("auditWorkorder", $user["enterprise_code"]);
        array_push($openidArray, $user["openid"]);
        $openidArray = array_unique($openidArray);
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            ApprovalResultNotify($notifyData);
        }
        $data["count"] = $count;
        echo json_encode($data);
    }
}